package com.memo4android;

import com.memo4android.model.AllNotesAdapter;
import com.memo4android.model.AppModel;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;

public class MainView extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
  private Intent intentAllTodos;
  private Intent intentNewNote;
  private Intent intentNewFolder;
  private Context context=this;
  private AllNotesAdapter allNotesAdapter;
  
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_notes);
        setTitle(this.getTitle() + " - HOME");
              
        //Ajout des dossiers la gridview
        GridView gridview = (GridView) findViewById(R.id.tableau_memo2);
        allNotesAdapter = new AllNotesAdapter(this);
        gridview.setAdapter(allNotesAdapter);
    
        //association du listener au bouton alltodos
        Button button_alltodos_n=(Button) findViewById(R.id.button_alltodos_n);
        button_alltodos_n.setOnClickListener(this);
        intentAllTodos = new Intent(this ,ToDoListsActivity.class);
        intentAllTodos.putExtra("label", "all todos");

        Button button_newNote=(Button)findViewById(R.id.button_ajouter_icone_n);
        button_newNote.setOnClickListener(this);
        
        intentNewNote= new Intent(this,NewNoteActivity.class);
        
        Button button_newFolder=(Button)findViewById(R.id.button_ajouter_dossier_n);
        button_newFolder.setOnClickListener(this);
        
        intentNewFolder=new Intent(this,NewNoteFolderActivity.class);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(hasFocus){
        	allNotesAdapter.notifyDataSetChanged();
        }
    }
    
    @Override
    public void onBackPressed(){
    	moveTaskToBack(true);	
    }
    
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
			
	     switch(v.getId()){
	     
		     case R.id.button_alltodos_n:
		    	 startActivity(intentAllTodos);
		    	 break;
		    	 
		     case R.id.button_ajouter_icone_n:
				AppModel model=AppModel.getInstance(context);
	
				 intentNewNote.putExtra("folderName",model.getHome().getTitre());
				 startActivity(intentNewNote);
				 break;
				 
		     case R.id.button_ajouter_dossier_n:
		    	 startActivity(intentNewFolder);
		    	    break;
	     }	
	} 	
}